package com.zhaotianhao.check.kafkaconsumer.kafka;

import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.listener.MessageListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @Author Z-7
 * @Date 2022/4/2
 */
@Slf4j
@Component
public class Kafka implements MessageListener<String, String> {
    @Resource
    private RedisTemplate redisTemplate;

    @Override
    @KafkaListener(topics = "combo")
    public void onMessage(ConsumerRecord<String, String> data) {
        String key = data.key();
        if (!redisTemplate.opsForValue().setIfAbsent(key,"",5, TimeUnit.HOURS)) {
            log.info("消息重复消费");
            return;
        }
        log.info("kafka正常消费");
        System.out.println(data.value());

        // 删除reids缓存
        //redisTemplate.delete("message:"+key);
    }
}
